// 
// Creative Software Group� - http://www.creativesoftwaregroup.com 
// Copyright (c) 2010-2013 
// by Creative Software Group Corporation 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;

namespace CSG.Modules.aNvarCharTest
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The EditaNvarCharTest class is used to manage content 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    partial class EditaNvarCharTest : PortalModuleBase
    {

        #region "Private Members"

        private int _RecordPk = Null.NullInteger;

        private void FillGrid()
        {
            // Add code here to create a controller, an list of info 
            //    objects, and bind the info object list to the grid.

            aNvarCharTestController myController = new aNvarCharTestController();
            List<aNvarCharTestInfo> myObjectList = new List<aNvarCharTestInfo>(myController.GetaNvarCharTests());
            gvCSG.DataSource = myObjectList;
            gvCSG.DataBind();

        }

        #endregion

        #region "Event Handlers"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// Page_Load runs when the control is loaded 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void Page_Load(object sender, System.EventArgs e)
        {
            try
            {

                // Determine RecordPk of aNvarCharTest to Update 
                if ((Request.QueryString["aNvarChar_PK"] != null))
                {
                    _RecordPk = Int32.Parse(Request.QueryString["aNvarChar_PK"]);
                }

                // If this is the first visit to the page, bind the role data to the datalist 
                if (Page.IsPostBack == false)
                {

                    // If you need to do anything to delete anything out side of the grid delete feature and want a confirmation, then uncomment the next line of code.
                    // Remember to uncomment the control in the ascx page for the edit view.
                    // cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');"); 

                    // If this is the first time to this page, fill the Grid View
                    FillGrid();

                    //if (!Null.IsNull(_RecordPk)) { 
                    //    // get content 
                    //    aNvarCharTestController objaNvarCharTests = new aNvarCharTestController(); 
                    //    aNvarCharTestInfo objaNvarCharTest = objaNvarCharTests.GetaNvarCharTest( _RecordPk); 
                    //    if ((objaNvarCharTest != null)) { 
                    //        // The table is loaded at this point.
                    //        // ViewGridviewBuilder.cs-GetViewCodeBehindLoadEventBodyScripter // See code on line 172 in EditGridviewBuilder.cs
                    //        // NEED TO RE-ADD THE ctlAudit code below
                    //        //ctlAudit.CreatedByUser = objaNvarCharTest.CreatedByUserName; 
                    //        //ctlAudit.CreatedDate = objaNvarCharTest.CreatedDate.ToString(); 
                    //    } 
                    //    else { 
                    //        // security violation attempt to access item not related to this Module 
                    //        Response.Redirect(Globals.NavigateURL(), true); 
                    //    } 
                    //} 
                    //else { 
                    //    cmdDelete.Visible = false; 
                    //    ctlAudit.Visible = false; 
                    //} 

                }
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdCancel_Click runs when the cancel button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdUpdate_Click runs when the update button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                // If you need to do anything to update anything out side of the grid update feature
                //   place the code here and uncomment the Update button code in the ascx page.

                // Redirect back to the portal home page 
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdDelete_Click runs when the delete button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                // If you need to do anything to delete anything out side of the grid delete feature
                //   place the code here and uncomment the Dekete button code in the ascx page.

                // Redirect back to the portal home page 
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void gvCSG_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // 
            // If you need to add, edit, or transform the data as it is being,
            //     bound to the row in the grid; add code here.
            // 

        }
        protected void gvCSG_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // If grid is not empty, the <Footer> will be shown.
            if (e.CommandName.Equals("Insert"))
            {
                aNvarCharTestInfo myInfoObject = new aNvarCharTestInfo();
                myInfoObject.nvarchar10 = Convert.ToString(((TextBox)gvCSG.FooterRow.Cells[1].FindControl("txtInsnvarchar10")).Text);
                myInfoObject.nvarchar25 = Convert.ToString(((TextBox)gvCSG.FooterRow.Cells[1].FindControl("txtInsnvarchar25")).Text);
                myInfoObject.nvarchar50 = Convert.ToString(((TextBox)gvCSG.FooterRow.Cells[1].FindControl("txtInsnvarchar50")).Text);
                myInfoObject.nvarcharMax = Convert.ToString(((TextBox)gvCSG.FooterRow.Cells[1].FindControl("txtInsnvarcharMax")).Text);
                myInfoObject.VarcharMax = Convert.ToString(((TextBox)gvCSG.FooterRow.Cells[1].FindControl("txtInsVarcharMax")).Text);
                aNvarCharTestController myController = new aNvarCharTestController();
                myController.AddaNvarCharTest(myInfoObject);

            }
            // If grid is empty, the <EmptyDataTemplate> will be shown.
            if (e.CommandName.Equals("Add"))
            {
                aNvarCharTestInfo myInfoObject = new aNvarCharTestInfo();
                GridViewRow gvr;
                Table tbl = (Table)gvCSG.Controls[0];
                gvr = (GridViewRow)tbl.Controls[0];
                TableCell emptyRowCell = gvr.Cells[0];
                myInfoObject.nvarchar10 = Convert.ToString(((TextBox)emptyRowCell.FindControl("txtAddnvarchar10")).Text);
                myInfoObject.nvarchar25 = Convert.ToString(((TextBox)emptyRowCell.FindControl("txtAddnvarchar25")).Text);
                myInfoObject.nvarchar50 = Convert.ToString(((TextBox)emptyRowCell.FindControl("txtAddnvarchar50")).Text);
                myInfoObject.nvarcharMax = Convert.ToString(((TextBox)emptyRowCell.FindControl("txtAddnvarcharMax")).Text);
                myInfoObject.VarcharMax = Convert.ToString(((TextBox)emptyRowCell.FindControl("txtAddVarcharMax")).Text);
                aNvarCharTestController myController = new aNvarCharTestController();
                myController.AddaNvarCharTest(myInfoObject);

            }
            // If grid is empty, the <EmptyDataTemplate> will be shown.
            if (e.CommandName.Equals("Update"))
            {
                aNvarCharTestInfo myInfoObject = new aNvarCharTestInfo();
                GridViewRow gvr;
                gvr = (GridViewRow)gvCSG.Rows[gvCSG.EditIndex];
                myInfoObject.aNvarChar_PK = Convert.ToInt32(((HiddenField)gvr.FindControl("txtAddaNvarChar_PK")).Value);
                myInfoObject.nvarchar10 = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarchar10")).Text);
                myInfoObject.nvarchar25 = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarchar25")).Text);
                myInfoObject.nvarchar50 = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarchar50")).Text);
                myInfoObject.nvarcharMax = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarcharMax")).Text);
                myInfoObject.VarcharMax = Convert.ToString(((TextBox)gvr.FindControl("txtEdtVarcharMax")).Text);
                aNvarCharTestController myController = new aNvarCharTestController();
                myController.UpdateaNvarCharTest(myInfoObject);

            }
            FillGrid();
        }
        protected void gvCSG_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvCSG.EditIndex = e.NewEditIndex;
            FillGrid();
        }
        protected void gvCSG_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            aNvarCharTestInfo myInfoObject = new aNvarCharTestInfo();
            GridViewRow gvr;
            gvr = (GridViewRow)gvCSG.Rows[gvCSG.EditIndex];
            myInfoObject.aNvarChar_PK = Convert.ToInt32(((HiddenField)gvr.FindControl("txtAddaNvarChar_PK")).Value);
            myInfoObject.nvarchar10 = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarchar10")).Text);
            myInfoObject.nvarchar25 = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarchar25")).Text);
            myInfoObject.nvarchar50 = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarchar50")).Text);
            myInfoObject.nvarcharMax = Convert.ToString(((TextBox)gvr.FindControl("txtEdtnvarcharMax")).Text);
            myInfoObject.VarcharMax = Convert.ToString(((TextBox)gvr.FindControl("txtEdtVarcharMax")).Text);
            aNvarCharTestController myController = new aNvarCharTestController();
            myController.UpdateaNvarCharTest(myInfoObject);

            gvCSG.EditIndex = -1;
            FillGrid();
        }
        protected void gvCSG_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvCSG.EditIndex = -1;
            FillGrid();
        }
        protected void gvCSG_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Int32 Id = Convert.ToInt32(gvCSG.DataKeys[e.RowIndex].Values[0].ToString());
            aNvarCharTestController myController = new aNvarCharTestController();
            myController.DeleteaNvarCharTest(Id);

            FillGrid();
        }

        #endregion

    }
}
